var games = document.querySelectorAll(".game");
console.log(games);
//添加div
var body = document.querySelector("body");
var _settlement = document.querySelector(".settlement");
for(let d = 0;d<(games.length-1);d++){
	var gameClone = games[d].cloneNode(true);
	body.insertBefore(gameClone,_settlement);
}
var newgames = document.querySelectorAll(".game");
console.log(newgames);
//封装选项卡
var f =0;
function change(f){
	click = true;
	for(let e = 0;e<newgames.length;e++){
		newgames[e].style.display = "none";
		newgames[f].style.display = "block";
	}
}
//----------------------------------------
//歌曲信息选择获取
var music = sessionStorage.getItem("Arr");
console.log(JSON.parse(music));
music = JSON.parse(music);
var s = 0; 
//封装歌曲播放
var audio = document.querySelector("audio");
function song(s){
	console.log(s);
	var _audio = music[s].audio;
	console.log(_audio);
	audio.src = _audio;
}
song(s);
//封装灯光颜色变化(有些页面不需要灯)
function light(){
	console.log(123321)
	var light = newgames[f].querySelector(".light");
	var lightImg = light.querySelector("img");
	var count = 1;
	var lightOne = "light.png";
	var lightTwo = "light2.png";
	setInterval(function time(){
		if(count == 1){
			lightImg.src = "img/"+lightTwo;
			count = 0;
		}else{
			lightImg.src = "img/"+lightOne;
			count = 1;
		}
	},500)
}
light();
//封装yaoyaoyao切换图片
function yao(){
	var yao = newgames[f].querySelector(".yaoyaoyao");
	var yaoImg = yao.querySelector("img");
	var count = 1;
	var yaoOne = "yaoyaoyao.png";
	var yaoTwo = "yaoyaoyao2.png";
	setInterval(function time2(){
		if(count == 1){
			yaoImg.src = "img/"+yaoTwo;
			count = 0;
		}else{
			yaoImg.src = "img/"+yaoOne;
			count = 1;
		}
	},500)
}
//封装tiangouqiehuantup
function tg(){
	var tg = newgames[f].querySelector(".tiangou");
	var tgImg = tg.querySelector("img");
	var count = 1;
	var tgOne = "tiangou.png";
	var tgTwo = "tiangou2.png";
	setInterval(function time3(){
		if(count == 1){
			tgImg.src = "img/"+tgTwo;
			count = 0;
		}else{
			tgImg.src = "img/"+tgOne;
			count = 1;
		}
	},500)
}
//封装倒计时
var j = 0;
let tim;
function Countdown(){
	clearInterval(tim);
	var span = newgames[f].querySelector("span");
	var arr = [];
	for(let i=60;i>=0;i--){
		arr.push(i);
	}
	console.log(j);
	tim = setInterval(function timer(){
		if(j==arr.length){
			console.log(1);
			audio.pause();
			f = 20;
			rank.innerText = "猜中"+y+"首";
			change(f);
			clearInterval(tim);
		}else{
			span.innerText = arr[j];
			j++;
		}
	},1000)
}
Countdown();
//结算页面的名次计算
var settlement = document.querySelector(".settlement");
var rank = settlement.querySelector(".ranking");
var y = 0;
//封装点击事件以及渲染答案和歌曲
var click = true;
function chickAndrender(){
	//获取p标签并用数组装起来
	var allP = newgames[f].querySelectorAll("p"); 
	var a = newgames[f].querySelector(".a");
	var b = newgames[f].querySelector(".b");
	var c = newgames[f].querySelector(".c");
	var chick = [];
	chick.push(a,b,c);
	//选项渲染
	for(let k=0;k<music[s].item.length;k++){
		console.log(newgames[f]);
		console.log(music)
		allP[k].innerText = music[s].item[k];
		//点击事件
		chick[k].addEventListener("touchend",function(e){
			if(k==music[s].answer&&click){
				click = false;
				y++;
				console.log(y);
				var yes = this.querySelector(".yes");
				yes.style.display = "inline-block";
				setTimeout(function(){
					if(f==19){
						f++;
						change(f);
						audio.pause();
						rank.innerText = "猜中"+y+"首";
					}else{
						f++;
						console.log(1);
						change(f);
						if(f==1){
							yao();
						}
						if(f==5){
							tg();
						}
						Countdown();
						s++;
						song(s);
						chickAndrender();
						console.log(f);
						if(f==1||f==2||f==4){
							light();
						}	
					}
				},800)
			}else if(click){
				click = false;
				var wrong = this.querySelector(".wrong");
				wrong.style.display = "inline-block";
				var yes = chick[music[s].answer].querySelector(".yes");
				yes.style.display = "inline-block";
				setTimeout(function(){
					if(f==19){
						f++;
						change(f);
						audio.pause();
						rank.innerText = "猜中"+y+"首";
					}else{
						f++;
						console.log(1);
						change(f);
						if(f==1||f==7||f==11||f==17){
							yao();
						}
						if(f==5||f==15){
							tg();
						}
						Countdown();
						s++;
						song(s);
						chickAndrender();
						console.log(f);
						if(f==1||f==2||f==4||f==6||f==7||f==8||f==9||f==10||f==11||f==12||f==14||f==16||f==17||f==18||f==19){
							light();
						}	
					}
				},800)
			}
		})
	}
}
chickAndrender();
jQuery(document).ready(function($) {
    audioAutoPlay('ad');
});
function audioAutoPlay(id){
    var audios = document.getElementById(id),
        play = function(){
            audios.play();
            document.removeEventListener("touchstart",play, false);
        };
   	console.log(audios)
    audios.play();
    //兼容微信
    document.addEventListener("WeixinJSBridgeReady", function () {
        play();
    }, false);
   
    //兼容易信
    document.addEventListener('YixinJSBridgeReady', function() {
        play();
    }, false);
    document.addEventListener("touchstart",play, false);
}